<template>
  <!-- // 分配角色 -->
  <div>
    <el-checkbox-group v-model="roleIds">
      <!-- label动态生成 -->
      <el-checkbox
        v-for="item in list"
        :key="item.id"
        :label="item.id"
      >{{ item.name }}</el-checkbox>

    </el-checkbox-group>

    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="closeDialog">取消</el-button>
    </div>
  </div>
</template>
<script>
import { getRoles } from '@/api/settings'
import { getUserDetail } from '@/api/user'
import { assignRoles } from '@/api/employees'

export default {
  props: {
    // 将父组件的员工id值传输过来
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      list: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    async hSubmit() {
      // 给员工分配角色
      try {
        await assignRoles({ id: this.curId, roleIds: this.roleIds })
        // 借助父组件关闭弹层
        this.$emit('success')
      } catch (error) {
        console.log(error, 'assignRoles')
      }
    },
    async loadRoles() {
      try {
        const res = await getRoles({ page: 1, pagesize: 100 })
        console.log('getRoles', res)
        // 保存数据
        // 获取员工个人详情
        const detail = await getUserDetail(this.curId)
        console.log(detail, 'getUserDetail')
        // 将当前员工的角色状态id赋值给多选组件（用于确认复选框的选中状态）
        this.roleIds = detail.data.roleIds
        this.list = res.data.rows
      } catch (err) {
        console.log('getRoles', err)
      }
    },
    closeDialog() {

    }
  }
}
</script>
